/**
 * 集中式的管理 API
 * @author rxliuli
 */

/**
 * 省份信息管理API
 */
const sysConfProvinceApi = new class SysConfProvinceApi extends BaseApi {
    constructor() {
        super('/api/sysConfProvince')
    }

    selectList = rx.cacheUtil.onceOfSameParam(super.selectList.bind(this), {identity: 'sysConfProvinceApi.selectList'})
}

/**
 * 市信息管理API
 */
const sysConfCityApi = new class SysConfCityApi extends BaseApi {
    constructor() {
        super('/api/sysConfCity')
    }

    selectList = rx.cacheUtil.onceOfSameParam(super.selectList.bind(this), {identity: 'sysConfCityApi.selectList'})
}

/**
 * 区信息管理API
 */
const sysConfAreaApi = new class SysConfAreaApi extends BaseApi {
    constructor() {
        super('/api/sysConfArea')
    }

    selectList = rx.cacheUtil.onceOfSameParam(super.selectList.bind(this), {identity: 'sysConfAreaApi.selectList'})
}


const provinceCityAreaApi = new class ProvinceCityAreaApi {
    /**
     * 查询省市区级联数据
     */
    async selectMap() {
        return {
            provinceMap: rx.arrayToMap(await sysConfProvinceApi.selectList(), ({id}) => id, ({name}) => name),
            cityMap: rx.groupBy(await sysConfCityApi.selectList(), ({provinceId}) => provinceId, (res, {id, name}) => res.set(id, name), () => new Map()),
            areaMap: rx.groupBy(await sysConfAreaApi.selectList(), ({cityId}) => cityId, (res, {id, name}) => res.set(id, name), () => new Map()),
        }
    }
}()
